package com.g7.dsp.forprotocol.services.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.g7.dsp.forprotocol.beans.LiquidLevel;
import com.g7.dsp.forprotocol.contexts.ProtocolContext;
import com.g7.dsp.forprotocol.services.Converter;
import com.g7.dsp.util.smart.LiquidResolveUtil;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class CtboxConverter implements Converter{
	//协议常量_油感
		public static final String OILFLAG = "2A 58 44";
		public static final String EXCELNAME = "Liquid";
		BufferedReader br = null;
		List<LiquidLevel> liquidLevelInfoList = new ArrayList<LiquidLevel>();
		private static WritableWorkbook wwb;
		private static WritableSheet ws;

		JTextField ja1 = new JTextField(20);
		ProtocolContext prt;

		// 需多个构造函数，提供不同的服务
		public CtboxConverter() {
			prt = ProtocolContext.getProtocolContext();
		}

		public void processConvert(){
			long start = System.currentTimeMillis();
			List<String> fileList = prt.getFileList();
			try{
			for (String file : fileList) {
				br = new BufferedReader(new FileReader(file));
				String dataline = null;
				while ((dataline = br.readLine()) != null) {
					if (dataline.contains(OILFLAG)) {
						String imei = getIMEI(dataline);
						byte[] a = formatMessage(dataline);
						LiquidResolveUtil.imei = imei;
						LiquidResolveUtil.data = a;
						liquidLevelInfoList.addAll(LiquidResolveUtil.getDataList());
						LiquidResolveUtil.data = null;
						LiquidResolveUtil.imei = null;
						LiquidResolveUtil.dataList = null;
					} else {
						continue;
					}
				}
			}
			}catch(IOException e){
				e.printStackTrace();
			}		
		

			if (liquidLevelInfoList != null && liquidLevelInfoList.size() > 0) {
				String outputDirPath = prt.getOutputFilePath();
				try {
					System.out.println(outputDirPath+"\\" + EXCELNAME + getNow() + ".xls");
					File excelFile = new File(outputDirPath+"\\" + EXCELNAME + getNow() + ".xls");
					wwb = Workbook.createWorkbook(excelFile);
					ws = wwb.createSheet("解析结果", 0);
					addExcelHeader(wwb, ws);
				} catch (ParseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            
				for (int j = 0; j < liquidLevelInfoList.size(); j++) {
					int i = j+1;
					LiquidLevel ll = liquidLevelInfoList.get(i-1);
					Label labe0 = new Label(0, i, ll.getImei());
					Label label = new Label(1, i, String.valueOf(ll.getGpsno()));
					Label labe2 = new Label(2, i, String.valueOf(ll.getVersion()));
					Label labe3 = new Label(3, i, String.valueOf(ll.getMainLevel()));
					Label labe4 = new Label(4, i, String.valueOf(ll.getRealMainLevel()));
					Label labe5 = new Label(5, i, String.valueOf(ll.getMainVolume()));
					Label labe6 = new Label(6, i, String.valueOf(ll.getSubLevel()));
					Label labe7 = new Label(7, i, String.valueOf(ll.getRealSubLevel()));
					Label labe8 = new Label(8, i, String.valueOf(ll.getSubVolume()));
					Label labe9 = new Label(9, i, String.valueOf(ll.getSignalIntensity()));
					Label labe10 = new Label(10, i, String.valueOf(ll.getSoftwareStatus()));
					Label labe11 = new Label(11, i, String.valueOf(ll.getHardwareStatus()));
					Label labe12 = new Label(12, i, String.valueOf(ll.getTemperature()));
					Label labe13 = new Label(13, i, String.valueOf(ll.getSpeed()));
					Label labe14 = new Label(14, i, String.valueOf(ll.getLng()));
					Label labe15 = new Label(15, i, String.valueOf(ll.getLat()));
					Label labe16 = new Label(16, i, String.valueOf(ll.getAcc()));
					Label labe17 = new Label(17, i, String.valueOf(ll.getTime()));
					Label labe18 = new Label(18, i, String.valueOf(ll.getCreateTime()));
					try {
						ws.addCell(labe0);
						ws.addCell(label);
						ws.addCell(labe2);
						ws.addCell(labe3);
						ws.addCell(labe4);
						ws.addCell(labe5);
						ws.addCell(labe6);
						ws.addCell(labe7);
						ws.addCell(labe8);
						ws.addCell(labe9);
						ws.addCell(labe10);
						ws.addCell(labe11);
						ws.addCell(labe12);
						ws.addCell(labe13);
						ws.addCell(labe14);
						ws.addCell(labe15);
						ws.addCell(labe16);
						ws.addCell(labe17);
						ws.addCell(labe18);
					} catch (WriteException e) {
						e.printStackTrace();
					}
				}
				
				try {
					wwb.write();
					wwb.close();
					JOptionPane.showMessageDialog(this.ja1, "转换成功！");
				} catch (WriteException e) {
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(this.ja1, "没有油感数据,请检查你的文件！");
			}
			long end = System.currentTimeMillis();
		}


		private byte[] formatMessage(String rawMessage) {
			int length = rawMessage.length();
			int msgStartIndex = 0;
			int msgEndIndex = 0;
			msgStartIndex = rawMessage.indexOf(OILFLAG);
			if (msgStartIndex > 0) {
				msgEndIndex = msgStartIndex + 245;
				byte[] a = LiquidResolveUtil.str2bytes(rawMessage.substring(msgStartIndex, msgEndIndex));
				return a;
			} else {
				return null;
			}
		}
		
		private String getIMEI(String rawMessage) {
			String imei=null;
			int msgStartIndexUp = 0;
			int msgStartIndexDown = 0;
			msgStartIndexUp = rawMessage.indexOf("<=");
			msgStartIndexDown = rawMessage.indexOf("=>");
			if (msgStartIndexUp > 0) {
				imei = rawMessage.substring(msgStartIndexUp+18, msgStartIndexUp+35).trim().replace(" ", "");
			}else if(msgStartIndexDown > 0){
				imei = rawMessage.substring(msgStartIndexDown+18, msgStartIndexDown+35).trim().replace(" ", "");
			}
			else {
				imei=null;
			}
			return imei;
		}
	    
		private void addExcelHeader(WritableWorkbook wwb,WritableSheet ws){
			    Label labe0 = new Label(0, 0, "imei");
				Label label = new Label(1, 0, "gpsno");
				Label labe2 = new Label(2, 0, "version");
				Label labe3 = new Label(3, 0, "mainLevel");
				Label labe4 = new Label(4, 0, "realMainLevel");
				Label labe5 = new Label(5, 0, "mainVolume");
				Label labe6 = new Label(6, 0, "subLevel");
				Label labe7 = new Label(7, 0, "realSubLevel");
				Label labe8 = new Label(8, 0, "subVolume");
				Label labe9 = new Label(9, 0, "signalIntensity");
				Label labe10 = new Label(10, 0, "softwareStatus");
				Label labe11 = new Label(11, 0, "hardwareStatus");
				Label labe12 = new Label(12, 0, "temperature");
				Label labe13 = new Label(13, 0, "speed");
				Label labe14 = new Label(14, 0, "lng");
				Label labe15 = new Label(15, 0, "lat");
				Label labe16 = new Label(16, 0, "acc");
				Label labe17 = new Label(17, 0, "time");
				Label labe18 = new Label(18, 0, "createTime");
				try {
				ws.addCell(labe0);
				ws.addCell(label);
				ws.addCell(labe2);
				ws.addCell(labe3);
				ws.addCell(labe4);
				ws.addCell(labe5);
				ws.addCell(labe6);
				ws.addCell(labe7);
				ws.addCell(labe8);
				ws.addCell(labe9);
				ws.addCell(labe10);
				ws.addCell(labe11);
				ws.addCell(labe12);
				ws.addCell(labe13);
				ws.addCell(labe14);
				ws.addCell(labe15);
				ws.addCell(labe16);
				ws.addCell(labe17);
				ws.addCell(labe18);
				} catch (WriteException e) {
					e.printStackTrace();
				}
	       }
		
		
		public String getNow() throws ParseException {
			Date date = new Date();
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HHmmss");
			return df.format(date).replace(" ", "");
		}
}
